package com.ruoyi.monitor.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.monitor.dao.entity.Camera;
import com.ruoyi.monitor.dao.entity.CameraInfo;
import com.ruoyi.monitor.dao.mapper.CameraMapper;
import com.ruoyi.monitor.dto.domain.CameraRich;
import com.ruoyi.monitor.dto.req.CameraQueryReqDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @desc: 监控设备管理
 * @author：cc
 * @date: 2024/7/25
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CameraService {

    private final CameraMapper cameraMapper;

    private final CameraInfoService cameraInfoService;

    /**
     * 条件查询设备列表
     * 会把设备下面的通道信息也查询出来
     */
    public List<CameraRich> queryList(CameraQueryReqDTO req, Pageable page) {
        Page<Object> pageHelperPage = null;
        if (page != null) {
            pageHelperPage = PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        List<Camera> cameras = cameraMapper.selectList(req);

        Page<CameraRich> pageResult = new Page<>();
        if (pageHelperPage != null) {
            pageResult.setPageNum(pageHelperPage.getPageNum());
            pageResult.setPageSize(pageHelperPage.getPageSize());
            pageResult.setTotal(pageHelperPage.getTotal());
            pageResult.setPages(pageHelperPage.getPages());
        }

        if (cameras.isEmpty()) {
            return pageResult;
        }
        List<String> serials = cameras.stream().map(Camera::getDeviceSerial).collect(Collectors.toList());

        List<CameraInfo> cameraInfos = cameraInfoService.queryBySerials(serials);
        Map<String, List<CameraInfo>> infoMap = cameraInfos.stream()
                .collect(Collectors.groupingBy(CameraInfo::getDeviceSerial));

        ArrayList<CameraRich> result = new ArrayList<>();
        for (Camera each : cameras) {
            CameraRich cameraRich = new CameraRich();
            cameraRich.setCamera(each);
            cameraRich.setCameraInfos(infoMap.get(each.getDeviceSerial()));
            result.add(cameraRich);
        }
        pageResult.addAll(result);

        return pageResult;
    }

    public int create(Camera camera) {
        camera.setCreateBy(SecurityUtils.getUsername());
        return cameraMapper.insert(camera);
    }

    @Transactional(rollbackFor = Exception.class)
    public int update(Camera camera) {
        camera.setDelFlag(null);

        String newSerial = camera.getDeviceSerial();
        if (StringUtils.isNotBlank(newSerial)) {
            // 如果修改了序列号, 需要同步修改通道的序列号
            Camera originalCamera = cameraMapper.selectByPrimaryKey(camera.getId());
            String originalSerial = originalCamera.getDeviceSerial();
            if (!originalSerial.equals(newSerial)) {
                cameraInfoService.updateDeviceSerial(originalSerial, newSerial);
            }
        }

        return cameraMapper.updateSelective(camera);
    }

    public int remove(Long id) {
        return cameraMapper.updateDelFlag(id);
    }

    /**
     * 通过id查询一个设备的详细信息
     */
    public CameraRich queryById(Long id) {
        Camera camera = cameraMapper.selectByPrimaryKey(id);
        List<CameraInfo> cameraInfos = cameraInfoService.queryBySerial(camera.getDeviceSerial());
        CameraRich cameraRich = new CameraRich();
        cameraRich.setCamera(camera);
        cameraRich.setCameraInfos(cameraInfos);
        return cameraRich;
    }
}
